const express = require("express");
const Upload = express.Router();
const multer = require("multer");

let storage = multer.diskStorage({
  destination(req, file, cb) {
    cb(null, "./static/images");
  },
  filename(req, file, cb) {
    // 切割文件名
    const splitFileName = file.originalname.split(".");
    // 获取文件后缀名
    const endName = splitFileName[splitFileName.length - 1];
    // 图片名命名为时间戳防止重复
    const date = Date.now() + parseInt(Math.random() * 10000);
    // 设置文件名
    cb(null, `${date}.${endName}`);
  },
});

let upload = multer({
  storage,
});

Upload.post("/", upload.single("avator"), require("./action/other/upload"));

module.exports = Upload;
